package com.sokal.easycare.modules.cms.entity;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.Length;

import com.google.common.collect.Lists;
import com.sokal.easycare.common.config.Global;
import com.sokal.easycare.common.persistence.DataEntity;
import com.sokal.easycare.common.utils.StringUtils;
import com.sokal.easycare.modules.sys.entity.MainUser;

/**
 * 评论Entity
 * @author ThinkGem
 * @version 2013-05-15
 */
public class Comment extends DataEntity<Comment> {

	private static final long serialVersionUID = 1L;
	private Category category;// 分类编号
	private String contentId;	// 归属分类内容的编号（Article.id、Photo.id、Download.id）
	private String title;	// 归属分类内容的标题（Article.title、Photo.title、Download.title）
	private String content; // 评论内容
	private String name; 	// 评论姓名
	private String ip; 		// 评论IP
	private Date createDate;// 评论时间
	private MainUser auditUser; // 审核人、留言者
	private Date auditDate;	// 审核时间、点赞时间
	private String delFlag;	// 删除标记删除标记（0：正常；1：删除；2：审核）
	private Integer pointPraise; //留言人对该内容点赞次数（一般限制为1次）
	private String parentId; //父级留言（可作为回复等的父级依据）
	private List<Comment> replyingMsgList = Lists.newArrayList(); //获取子表（回复）列表

	public Comment() {
		super();
		this.delFlag = DEL_FLAG_NORMAL;
		this.pointPraise = 0;
	}
	
	public Comment(String id){
		this();
		this.id = id;
	}
	
	public Comment(Category category){
		this();
		this.category = category;
	}

	public Comment(String contentId, String parentId){
		this.delFlag = DEL_FLAG_NORMAL;
		this.contentId = contentId;
		this.parentId = parentId;
	}
	
	/**
	 * 插入或更新之前执行方法，需要手动调用
	 */
	@Override
	public void preInsert() {
		// 不限制ID为UUID，调用setIsNewRecord()使用自定义ID
		if (!this.isNewRecord) {
			setId(UUID.randomUUID().toString().replaceAll("-", ""));
		}
		if (StringUtils.isNotBlank(content)) {
			this.createDate = new Date();
		}
		if (pointPraise > 0) {
			this.auditDate = new Date();
		}
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	@NotNull
	public String getContentId() {
		return contentId;
	}

	public void setContentId(String contentId) {
		this.contentId = contentId;
	}

	@Length(min=1, max=255)
	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}
	
	@Length(min=1, max=255)
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}
	
	@Length(min=1, max=100)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}


	public MainUser getAuditUser() {
		if (auditUser != null) {
			if (StringUtils.isNotBlank(auditUser.getPortraitPhoto())) {
				auditUser.setPortraitPhoto(Global.getConfig("fastdfs_file_url")+auditUser.getPortraitPhoto());
			}
		}
		return auditUser;
	}

	public void setAuditUser(MainUser auditUser) {
		this.auditUser = auditUser;
	}

	public Date getAuditDate() {
		return auditDate;
	}

	public void setAuditDate(Date auditDate) {
		this.auditDate = auditDate;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	@Length(min=1, max=1)
	public String getDelFlag() {
		return delFlag;
	}

	public void setDelFlag(String delFlag) {
		this.delFlag = delFlag;
	}

	public Integer getPointPraise() {
		return pointPraise;
	}

	public void setPointPraise(Integer pointPraise) {
		this.pointPraise = pointPraise;
	}

	public String getParentId() {
		return parentId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	public List<Comment> getReplyingMsgList() {
		return replyingMsgList;
	}

	public void setReplyingMsgList(List<Comment> replyingMsgList) {
		this.replyingMsgList = replyingMsgList;
	}

}